import globals from 'globals'
import { defineFlatConfig } from 'eslint-define-config'
import js from '@eslint/js'
/** ESLint 核心插件 */
import * as vueParser from 'vue-eslint-parser'
import vuePlugin from 'eslint-plugin-vue'
import prettierConfig from 'eslint-config-prettier'
import prettierPlugin from 'eslint-plugin-prettier'
import * as tsParser from '@typescript-eslint/parser'
import tsPlugin from '@typescript-eslint/eslint-plugin'

export default defineFlatConfig([
  /** 通用 js 配置 */
  {
    ...js.configs.recommended,
    ignores: ['**/.*', 'dist/*', '*.d.ts', 'public/*', 'src/assets/**', '.npmrc', 'release', 'electron', 'config'],
    languageOptions: {
      globals: {
        ...globals.browser,
        ...globals.node,
        uni: 'readonly',
        wx: 'readonly',
        WechatMiniprogram: 'readonly',
        getCurrentPages: 'readonly',
        getApp: 'readonly',
        Nullable: 'readonly',
        ElRef: 'readonly',
        Recordable: 'readonly',
        ImportMetaEnv: 'readonly',
        ViteEnv: 'readonly',
        ServerConfigs: 'readonly',
        ElLoading: 'writable',
        ElMessage: 'writable',
        ElMessageBox: 'writable',
        ElNotification: 'writable'
      }
    },
    plugins: {
      prettier: prettierPlugin
    },
    rules: {
      ...prettierConfig.rules,
      ...prettierPlugin.configs.recommended.rules,
      'no-debug': 'off', // 禁止 debugger
      'no-unused-vars': 'off', // 允许未使用的变量，以 _ 开头的变量不检查
      'prettier/prettier': [
        'error',
        {
          endOfLine: 'auto' // 自动识别换行符
        }
      ]
    }
  },
  /** ts文件配置 */
  {
    files: ['**/*.?([cm])ts', '**/*.?([cm])tsx'],
    ignores: ['node_modules', 'dist', 'public', '.npmrc', 'release', 'electron', 'config'],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        sourceType: 'module',
        warnOnUnsupportedTypeScriptVersion: false
      }
    },
    plugins: {
      '@typescript-eslint': tsPlugin
    },
    rules: {
      ...tsPlugin.configs.strict.rules,
      '@typescript-eslint/ban-types': 'off', // 禁止特定类型
      '@typescript-eslint/no-redeclare': 'error', // 禁止重复声明
      '@typescript-eslint/ban-ts-comment': 'off', // 禁止特定注释
      '@typescript-eslint/no-explicit-any': 'off', // 禁止使用 any
      '@typescript-eslint/prefer-as-const': 'warn', // 使用 as const 替代 as 'const'
      '@typescript-eslint/no-empty-function': 'off', // 禁止空函数
      '@typescript-eslint/no-non-null-assertion': 'off', // 禁止非空断言
      '@typescript-eslint/no-import-type-side-effects': 'error', // 禁止导入类型产生副作用
      '@typescript-eslint/explicit-module-boundary-types': 'off', // 显式函数返回类型
      '@typescript-eslint/consistent-type-imports': ['error', { disallowTypeAnnotations: false, fixStyle: 'inline-type-imports' }],
      // 一致的类型导入
      '@typescript-eslint/prefer-literal-enum-member': 'off', // 使用字面量枚举成员
      '@typescript-eslint/no-unused-expressions': 'off',
      '@typescript-eslint/no-extraneous-class': 'off',
      '@typescript-eslint/no-useless-constructor': 'off',
      '@typescript-eslint/no-dynamic-delete': 'off',
      '@typescript-eslint/no-unsafe-function-type': 'off',
      '@typescript-eslint/ban-ts-comment': 'off',
      '@typescript-eslint/no-empty-object-type': 'off',
      '@typescript-eslint/unified-signatures': 'off',
      '@typescript-eslint/no-redeclare': 'off',
      '@typescript-eslint/no-duplicate-enum-values': 'off',
      '@typescript-eslint/no-require-import': 'off',
      '@typescript-eslint/no-unused-vars': 'off'
    }
  },
  /** ts 类型文件配置 */
  {
    files: ['**/*.d.ts'],
    ignores: ['node_modules', 'dist', 'public'],
    rules: {
      'eslint-comments/no-unlimited-disable': 'off',
      'import/no-duplicates': 'off',
      'unused-imports/no-unused-vars': 'off'
    }
  },
  // JavaScript 配置（包含 commonjs）
  {
    files: ['**/*.?([cm])js'],
    ignores: ['node_modules', 'dist', 'public', '.npmrc', 'release', 'electron', 'config'],
    rules: {
      '@typescript-eslint/no-require-imports': 'off', // 禁止 require
      '@typescript-eslint/no-var-requires': 'off' // 禁止 require
    }
  },
  // Vue 文件配置
  {
    files: ['**/*.vue'],
    ignores: ['node_modules', 'dist', 'public'],
    languageOptions: {
      globals: {
        $: 'readonly',
        $$: 'readonly',
        $computed: 'readonly',
        $customRef: 'readonly',
        $ref: 'readonly',
        $shallowRef: 'readonly',
        $toRef: 'readonly'
      },
      parser: vueParser,
      parserOptions: {
        extraFileExtensions: ['.vue'],
        parser: '@typescript-eslint/parser',
        sourceType: 'module',
        ecmaVersion: 'latest',
        /** 允许在.vue 文件中使用 JSX */
        ecmaFeatures: {
          jsx: true
        }
      }
    },
    plugins: {
      vue: vuePlugin
    },
    processor: vuePlugin.processors['.vue'],
    rules: {
      ...vuePlugin.configs.base.rules, // Vue 基础配置
      ...vuePlugin.configs['vue3-essential'].rules, // Vue3 基础配置
      ...vuePlugin.configs['vue3-recommended'].rules, // Vue3 推荐配置
      'no-undef': 'off',
      'no-unused-vars': 'off',
      'vue/no-v-html': 'off',
      'vue/require-default-prop': 'off',
      'vue/require-explicit-emits': 'off',
      'vue/multi-word-component-names': 'off',
      'vue/no-setup-props-reactivity-loss': 'off',
      '@typescript-eslint/no-unused-vars': 'off',
      'vue/html-self-closing': [
        'error',
        {
          html: {
            void: 'always',
            normal: 'always',
            component: 'always'
          },
          svg: 'always',
          math: 'always'
        }
      ], // 自闭合标签
      'vue/no-use-v-if-with-v-for': 'off',
      'vue/valid-v-for': 'off',
      'vue/require-valid-default-prop': 'off',
      '@typescript-eslint/ban-ts-comment': 'off'
    }
  }
])
